home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_chest.arc / PCXARRAY.C < prev    next >
Text File  |  1990-10-12  |  4KB  |  153 lines

  1. /* pcxarray.c
  2.    by Bill Buckels 1990
  3.  
  4.    converts a CGA COMPATIBLE .PCX screen dump into a
  5.    C-LANGUAGE CHARACTER ARRAY HEADER OF THE SAME NAME
  6.    but having the extension ".c"
  7.  
  8.    the graphic is embedded and either compiled seperately
  9.    as an object file and declared external & linked to
  10.    or alternately is used as a header.
  11.  
  12.    the graphic is then unpacked directly into a screen buffer,
  13.    or alternately into the frame buffer without disk i/o.
  14.  
  15. */
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <io.h>
  19. #include <string.h>
  20.  
  21.  
  22.  
  23. unsigned int byteword(unsigned char a, unsigned char b){return b<<8|a;}
  24. unsigned char pcxheader[128];
  25.  
  26. int checkforpcx(char *name)
  27. {
  28.     FILE *fp; /* reads a ZSOFT .PCX header but ignores the color map */
  29.     int i;    /* we only want CGA compatible full screens.           */
  30.  
  31.     unsigned int zsoft,version,codetype,pixbits;
  32.     unsigned int xmin, ymin, xmax, ymax;
  33.     unsigned int hres, vres;
  34.     unsigned int no_planes, bytesperline;
  35.     int invalid = -1, valid = 0, status=valid;
  36.  
  37.     /* read the file header */
  38.     if((fp=fopen(name,"rb"))==NULL)return -1;
  39.     for(i=0;i!=128;i++)pcxheader[i]=fgetc(fp);
  40.     fclose(fp);
  41.  
  42.     zsoft   =pcxheader[0];
  43.     version =pcxheader[1];
  44.     codetype=pcxheader[2];
  45.     pixbits =pcxheader[3];
  46.  
  47.     if(zsoft!=10)        status = invalid;
  48.     if(codetype!=1)      status = invalid;
  49.     if(pixbits != 2 )
  50.        if(pixbits != 1)  status = invalid;
  51.  
  52.     xmin=byteword(pcxheader[4],pcxheader[5]);
  53.     ymin=byteword(pcxheader[6],pcxheader[7]);
  54.     xmax=byteword(pcxheader[8],pcxheader[9]);
  55.     ymax=byteword(pcxheader[10],pcxheader[11]);
  56.     hres=byteword(pcxheader[12],pcxheader[13]);
  57.     vres=byteword(pcxheader[14],pcxheader[15]);
  58.     no_planes   =pcxheader[65];
  59.     bytesperline=byteword(pcxheader[66],pcxheader[67]);
  60.     if(xmin != 0  )      status = invalid;
  61.     if(ymin != 0  )      status = invalid;
  62.     if(xmax != 319)
  63.        if(xmax!=639)     status = invalid;
  64.     if(ymax != 199)      status = invalid;
  65.     if(hres != 320)
  66.        if(hres!=640)     status = invalid;
  67.     if(vres != 200)      status = invalid;
  68.     if(no_planes!=1)     status = invalid;
  69.     if(bytesperline !=80)status = invalid;
  70.     /* we can ignore the color map since we        */
  71.     /* are limiting ourselves to CGA modes         */
  72.     /* so we will not handle over 2-bits per pixel */
  73.     return status;
  74.  
  75. }
  76.  
  77.  
  78.  
  79. char separators[]=" .\n";
  80.  
  81. int PCXARRAY(char *pcxfilename)
  82. {
  83.     unsigned int packet=16,width=0;
  84.     FILE *fp,*fp2;
  85.     long wordcount,target;
  86.     char buffer[128];
  87.     char  name[128];
  88.     char  name2[128];
  89.     char *wordptr;
  90.  
  91.  
  92.     strcpy(buffer,pcxfilename);
  93.     wordptr=strtok(buffer,separators);
  94.     strcpy(name,buffer);
  95.     strcat(name,".PCX");
  96.     strcpy(name2,buffer);
  97.     strcat(name2,".c");
  98.  
  99.     if(checkforpcx(name)==-1)return-1;
  100.     fp = fopen(name,"rb");
  101.     fp2 = fopen(name2,"w");
  102.  
  103.     printf("\nInput File : %s",name);
  104.     printf("\nOutput File: %s\n",name2);
  105.  
  106.     fprintf(fp2,
  107.  "/* Character Array of .PCX format Run Length Full Screen CGA Graphic */\n");
  108.     fprintf(fp2,
  109.  "/* .PCX Input File Name was %s */\n\n",name);
  110.     fprintf(fp2,
  111.  "/* The BYTE COUNT Descriptor Integer precedes the Character Array.*/\n\n");
  112.  
  113.     target = filelength(fileno(fp));
  114.     for(wordcount=0;wordcount<128;wordcount++)fgetc(fp);
  115.  
  116.     fprintf(fp2,"int %s",buffer);
  117.     fprintf(fp2,"_SIZE = %ld ;\n\n",(target-128l));
  118.     fprintf(fp2,"unsigned char far %s[%ld]={\n",buffer,(target-128l));
  119.  
  120.     do{
  121.         fprintf(fp2,"%3d",fgetc(fp));
  122.         wordcount++;
  123.         if(wordcount !=target)fprintf(fp2,", ");
  124.         else fprintf(fp2,"};");
  125.         if(width++==packet){fprintf(fp2,"\n");
  126.                             width=0;}
  127.                             }while(wordcount!=target);
  128.         fclose(fp);
  129.         fclose(fp2);
  130.         return(0);
  131. }
  132.  
  133. char *usage[]={
  134. "PCXARRAY(C) Copyright by Bill Buckels 1990\n",
  135. "is not a valid CGA Full Screen .PCX file.\n",
  136. "Usage is \"PCXARRAY [PCXFILENAME]\"\n",
  137. "Done!\n"};
  138.  
  139.  
  140. main(int argc,char *argv[])
  141. {
  142.     printf("%s",usage[0]);
  143.     switch (argc)
  144.         {
  145.          case 2:  if(PCXARRAY(argv[1])==0)break;
  146.                   printf("%s %s",argv[1],usage[1]);
  147.          default: printf("%s",usage[2]);
  148.                   exit(0);
  149.         }
  150.     printf("%s",usage[3]);
  151.     exit(0);
  152. }
  153.